package com.at.controller.admin;

import com.at.domain.Blog;
import com.at.domain.BlogQuery;
import com.at.domain.User;
import com.at.service.BlogService;
import com.at.service.TagService;
import com.at.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author : code1997
 * @date :2020-09-2020/9/27 21:50
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    /**
     * 用于替换返回的路径
     */
    private static final String INPUT="admin/blogs-input";
    private static final String LIST="admin/blogs";
    private static final String REDIRECT_LIST="redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog,Model model){
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable,blog));
        return LIST;

    }
    @PostMapping("/blogs/search")
    public String blogsByCondition(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog, Model model){
        System.out.println(blog);
        model.addAttribute("page", blogService.listBlog(pageable,blog));
        /*只返回一个片段，来实现局部的渲染，而不会刷新其他的值*/
        return "admin/blogs::blogList";
    }

    @GetMapping("/blogs/input")
    public String toBlogInputPage(Model model){
        System.out.println("跳转到blogs的新增页面");
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    @GetMapping("/blogs/{id}/input")
    public String toBlogEditInputPage(@PathVariable Long id,Model model){
        System.out.println("跳转到blogs的修改页面");
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
        Blog blog = blogService.getBolg(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }

    /**
     * 这里只做了前端的校验
     * @param blog
     * @param session
     * @return
     */
    @PostMapping("/blogs")
    public String addOrEditBlog(Blog blog, HttpSession session, RedirectAttributes attributes){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog tempBlog ;
        if(blog.getId()==null){
            tempBlog=blogService.saveBlog(blog);
        }else {
            tempBlog=blogService.updateBlog(blog.getId(), blog);
        }
        if (tempBlog==null){
            attributes.addFlashAttribute("message", "新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return REDIRECT_LIST;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功！");
        return REDIRECT_LIST;
    }


}
